﻿using System;
using System.Collections.Generic;
using System.Linq;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Epitech.Krisk.Xna.UI.Game.InGame.Gameplay;
using Epitech.Krisk.Xna.UI.Game.Utils;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace Epitech.Krisk.Xna.UI.Game.InGame
{
    public class InGameScreen : GameScreen
    {
        readonly List<AGameObject> _objects;
        ContentManager _content;
        private readonly GamePlay _gamePlay;

        public InGameScreen(int nbplayer, GamePlayType gameType)
        {
            Music.GetInstance().setMusic("Music/BackgroundMusic");
            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            _gamePlay = new GamePlay(nbplayer, gameType);

            _objects = new List<AGameObject>();
        }

        public InGameScreen(String gamename)
        {
            Music.GetInstance().setMusic("Music/BackgroundMusic");
            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            _gamePlay = new GamePlay(GamePlayType.None);

            _gamename = gamename;

            _objects = new List<AGameObject>();
        }

        public override void LoadContent()
        {
            _gamePlay.ScreenManager = ScreenManager;
            _content = new ContentManager(ScreenManager.Game.Services, "Content");


            _objects.Add(new RenderMovingBackground("Game/GameMap"));
            _objects.Last().Content = _content;
            _objects.Last().SpriteBatch = ScreenManager.SpriteBatch;
            _objects.Last().ViewPort = ScreenManager.GraphicsDevice.Viewport;
            _menuGame = new MenuGame(this, _gamePlay);
            _menuGame.Content = _content;
            _menuGame.SpriteBatch = ScreenManager.SpriteBatch;
            _menuGame.ViewPort = ScreenManager.GraphicsDevice.Viewport;
            _gamePlay.Content = _content;
            _gamePlay.SpriteBatch = ScreenManager.SpriteBatch;
            Continent.CreateContinentAndTerritory();
            Territory.RecupListTerritory();

            foreach (var territory in Territory.Territories)
            {
                territory.Content = _content;
                territory.SpriteBatch = ScreenManager.SpriteBatch;
                territory.ViewPort = ScreenManager.GraphicsDevice.Viewport;
            }

            //_objects.AddRange(Territory.Territories);
            _gamePlay.ViewPort = ScreenManager.GraphicsDevice.Viewport;



            _objects.Add(_gamePlay);

            
            foreach (AGameObject gameObject in _objects)
            {
                gameObject.LoadContent();
            }
            if (!String.IsNullOrEmpty(_gamename))
                ChargeAndSaveGame.Charge(_gamename, _gamePlay);
            _menuGame.LoadContent();
            foreach (var player in _gamePlay.Players)
            {
                foreach (KeyValuePair<Territory, Army> pair in player.OwnTerritory)
                {
                    pair.Key.LoadContent();
                }
            }
        }

        public override void UnloadContent()
        {
            foreach (AGameObject gameObject in _objects)
            {
                gameObject.UnloadContent();
            }
            _menuGame.UnloadContent();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            foreach (AGameObject gameObject in _objects)
            {
                gameObject.ViewPort = ScreenManager.GraphicsDevice.Viewport;
                gameObject.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            }
            _menuGame.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            base.Update(gameTime, otherScreenHasFocus, false);
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (AGameObject gameObject in _objects)
            {
                if (gameObject != _gamePlay)
                    gameObject.Draw(gameTime);
            }
            ToolTip tmp = null;
            foreach (var player in _gamePlay.Players)
            {
                foreach (KeyValuePair<Territory, Army> pair in player.OwnTerritory)
                {
                    pair.Key.Draw(gameTime);
                    if (pair.Key.ToolTip != null)
                        tmp = pair.Key.ToolTip;

                }
            }
            if (tmp != null) tmp.Draw(gameTime);
            _gamePlay.Draw(gameTime);
#if DEBUG
            ScreenManager.SpriteBatch.Begin();
            ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, _pos.ToString(), Vector2.Zero, Color.Black);
            ScreenManager.SpriteBatch.End();
#endif
            _menuGame.Draw(gameTime);
        }

        private Vector2 _pos;
        private MenuGame _menuGame;
        private readonly string _gamename = String.Empty;

        public override void HandleInput(InputState input)
        {
#if DEBUG
            _pos = input.MousePosition();
#endif
            foreach (var player in _gamePlay.Players)
            {
                foreach (KeyValuePair<Territory, Army> pair in player.OwnTerritory)
                {
                    pair.Key.HandleInput(input);
                }
            }
            PlayerIndex tmp;
            if (input.IsNewKeyPress(Keys.Escape, null, out tmp)
                || input.IsNewButtonPress(Buttons.Start, null, out tmp))
                ScreenManager.AddScreen(new Pause(_gamePlay), null);
            foreach (AGameObject gameObject in _objects)
            {
                gameObject.HandleInput(input);
            }
            _menuGame.HandleInput(input);
        }
    }
}
